#include <REGX52.h>
#include "DS1302.h"
#include "LCD1602.h"
#include "SoleKey.h"
#include "Timer.h"

//Key:独立按键键码  Change:是否修改时间模式  Select:修改时间位  TimeFlashFlag:修改时间修改位
unsigned char Key, Change, Select, TimeFlashFlag;
/**
  * @brief		显示时钟时间
  * @parm		无
  * @retval		无
  */
void ShowTime()
{
	//先读取最新时间，写入DS1302_Time数组
	DS1302_ReadTime();
	//显示时间
	LCD_ShowNum(1, 1, DS1302_Time[0], 2);
	LCD_ShowNum(1, 4, DS1302_Time[1], 2);
	LCD_ShowNum(1, 7, DS1302_Time[2], 2);
	LCD_ShowNum(2, 1, DS1302_Time[3], 2);
	LCD_ShowNum(2, 4, DS1302_Time[4], 2);
	LCD_ShowNum(2, 7, DS1302_Time[5], 2);
}
/**
  * @brief		单独封装改变日期，当年月日改变时，都要对日进行判断
  * @parm		无
  * @retval		无
  */
void ChangeDate()
{
	unsigned char Year = DS1302_Time[0], Month = DS1302_Time[1];
	if(Select < 3)//年月日修改才有可能影响日
	{
		if(Key == 3)
		{
			if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12){
				if(DS1302_Time[2] > 31)
					DS1302_Time[2] = 1;
			}
			else if(Month == 4 || Month == 6 || Month == 9 || Month == 11){
				if(DS1302_Time[2] > 30)
					DS1302_Time[2] = 1;
			}
			else if(Month == 2){
				if(Year % 4 == 0){
					if(DS1302_Time[2] > 29)
						DS1302_Time[2] = 1;
				}
				else{
					if(DS1302_Time[2] > 28)
						DS1302_Time[2] = 1;
				}
			}
		}
		else if(Key == 4)
		{
			if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12){
				if(DS1302_Time[2] < 1)
					DS1302_Time[2] = 31;
			}
			else if(Month == 4 || Month == 6 || Month == 9 || Month == 11){
				if(DS1302_Time[2] < 1 || DS1302_Time[2] > 30) //还要处理因年月改变而日期溢出的情况
					DS1302_Time[2] = 30;
			}
			else if(Month == 2){
				if(Year % 4 == 0){
					if(DS1302_Time[2] < 1 || DS1302_Time[2] > 29)//还要处理因年月改变而日期溢出的情况
						DS1302_Time[2] = 29;
				}
				else{
					if(DS1302_Time[2] < 1 || DS1302_Time[2] > 28)//还要处理因年月改变而日期溢出的情况
						DS1302_Time[2] = 28;
				}
			}
		}
	}
}
/**
  * @brief		修改时间
  * @parm		无
  * @retval		无
  */
void ChangeTime()
{
	//显示当前选择哪个
	LCD_ShowNum(2, 10, Select, 1);
	if(Key == 2)//切换要修改的时间部分
		Select = (Select + 1) % 6;
	else if(Key == 3)//时间+1
	{
		DS1302_Time[Select]++;
		if(Select == 0 && DS1302_Time[Select] > 99)//改变年
			DS1302_Time[Select] = 0;
		else if(Select == 1 && DS1302_Time[Select] > 12)//改变月
			DS1302_Time[Select] = 1;
		else if(Select == 3 && DS1302_Time[Select] > 23)//改变小时
			DS1302_Time[Select] = 0;
		else if(Select == 4 && DS1302_Time[Select] > 59)//改变分钟
			DS1302_Time[Select] = 0;
		else if(Select == 5 && DS1302_Time[Select] > 59)//改变秒
			DS1302_Time[Select] = 0;
		ChangeDate();//年月日修改都可能要修改日
	}
	else if(Key == 4)//时间-1
	{
		DS1302_Time[Select]--;
		if(Select == 0 && DS1302_Time[Select] < 0)//改变年
			DS1302_Time[Select] = 99;
		else if(Select == 1 && DS1302_Time[Select] < 1)//改变月
			DS1302_Time[Select] = 12;
		else if(Select == 3 && DS1302_Time[Select] < 0)//改变小时
			DS1302_Time[Select] = 23;
		else if(Select == 4 && DS1302_Time[Select] < 0)//改变分钟
			DS1302_Time[Select] = 59;
		else if(Select == 5 && DS1302_Time[Select] < 0)//改变秒
			DS1302_Time[Select] = 59;
		ChangeDate();//年月日修改都可能要修改日
	}
	//闪烁显示
	if(Select == 0 && TimeFlashFlag){LCD_ShowString(1, 1, "  ");}
	else{LCD_ShowNum(1, 1, DS1302_Time[0], 2);}
	if(Select == 1 && TimeFlashFlag){LCD_ShowString(1, 4, "  ");}
	else{LCD_ShowNum(1, 4, DS1302_Time[1], 2);}
	if(Select == 2 && TimeFlashFlag){LCD_ShowString(1, 7, "  ");}
	else{LCD_ShowNum(1, 7, DS1302_Time[2], 2);}
	if(Select == 3 && TimeFlashFlag){LCD_ShowString(2, 1, "  ");}
	else{LCD_ShowNum(2, 1, DS1302_Time[3], 2);}
	if(Select == 4 && TimeFlashFlag){LCD_ShowString(2, 4, "  ");}
	else{LCD_ShowNum(2, 4, DS1302_Time[4], 2);}
	if(Select == 5 && TimeFlashFlag){LCD_ShowString(2, 7, "  ");}
	else{LCD_ShowNum(2, 7, DS1302_Time[5], 2);}
}
//实现闪烁
void Timer0_Rountine(void) interrupt 1
{
	static unsigned int timeCount = 0;
	TH0 = 64535 / 256;		//高8位
	TL0 = 64535 % 256 + 1;		//低8位
	timeCount++;
	if(timeCount >= 500)
	{
		TimeFlashFlag = !TimeFlashFlag;
		timeCount = 0;
	}
}

void main()
{
	LCD_Init();
	DS1302_Init();
	Timer0_Init();
	LCD_ShowString(1, 1, "  -  -  ");
	LCD_ShowString(2, 1, "  :  :  ");
	DS1302_SetTime();
    while(1)
    {
		Key = soleKey();
		if(Key == 1)
		{
			if(Change){
				Change = 0;
				DS1302_SetTime();//将数组时间写入DS1302
				LCD_ShowString(2, 10, " ");
			}
			else{
				Change = 1;
				Select = 0;//重置选择位
			}
		}
		if(Change)//修改时间
			ChangeTime();
		else//显示时间
			ShowTime();
    }
}